<template>
  <div ref="wrapper">
  <div class="right_leader">
      <!-- cities是map对象，index就是键值对的键 -->
      <div class="city_leader" v-for="item of letters" :key="item" 
      :ref="item"
      @click="handleClick"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      >{{item}}</div>
     
      
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
export default {
  name: "aleader",
  props: {
    cities: Object
  },
  data() {
    return {
      timer: "",
      touchStatus: false,
      startY: 0
    };
  },
  computed: {
    letters() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    }
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  methods: {
    handleClick(e) {
      this.$emit("changeLeader", e.target.innerText);
      // console.log(e.target.innerText);
      //子组件通过emit事件将点击到的字母传给父组件City，父组件再传给子组件list，子组件list通过监听letter的变化，scroll到对应到列表
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 160;
          const item_index = Math.floor((touchY - this.startY) / 20);
          if (item_index >= 0 && item_index < this.letters.length) {
            this.$emit("changeLeader", this.letters[item_index]);
          }
        }, 16);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.right_leader {
  position: absolute;
  right: 0;
  bottom: 0;
  top: 2rem;
  z-index: 3;
  color: #00bcd4;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0.1rem;
  color: #fff;
  overflow: hidden;
}

.city_leader {
  line-height: 0.4rem;
  color: #00bcd4;
}
</style>